import { Response, Request } from 'express'
import { User } from '../../src/typography/interface/types/user'
import { getTokenKey, getToken } from '../../src/typography/kit/cookie'

/**
 * 构建子系统 key
 * @param id 子系统 id
 * @return 子系统 key
 */
const buildSubsystemKey = (id: string) => id

/**
 * 构建角色 key
 * @param id 角色 id
 * @return 角色 key
 */
const buildRoleKey = (id: string) => id

/**
 * 构建资源 key
 * @param type 资源类型
 * @param id 资源 id
 * @return 资源 key
 */
const buildResourceKey = (type: 'route', id: string) => `${type}-${id}`

const subsystemKeys: string[] = [
  buildSubsystemKey('PIT'),
  buildSubsystemKey('FTT'),
  buildSubsystemKey('FTT_BASIC'),
  buildSubsystemKey('FTT_VAT'),
  buildSubsystemKey('FTT_CIT'),
  buildSubsystemKey('SBS')
]

const resourceKeys: string[] = [
  buildResourceKey('route', 'PIT-BasicData'),
  buildResourceKey('route', 'PIT-Enterprise'),
  buildResourceKey('route', 'PIT-Staff'),
  buildResourceKey('route', 'PIT-PayFiducial'),
  buildResourceKey('route', 'PIT-PayRatio'),
  buildResourceKey('route', 'PIT-TaxStair'),
  buildResourceKey('route', 'PIT-AdditionalDeduct'),
  buildResourceKey('route', 'PIT-ChildEducate'),
  buildResourceKey('route', 'PIT-AdultEducate'),
  buildResourceKey('route', 'PIT-HouseLoan'),
  buildResourceKey('route', 'PIT-HouseRent'),
  buildResourceKey('route', 'PIT-SupportAged'),
  buildResourceKey('route', 'PIT-Medical'),
  buildResourceKey('route', 'PIT-Calc'),
  buildResourceKey('route', 'PIT-SalaryGather'),
  buildResourceKey('route', 'PIT-SalaryConfirm'),
  buildResourceKey('route', 'PIT-PayrollConfirm'),
  buildResourceKey('route', 'PIT-PayrollDoubt'),
  buildResourceKey('route', 'PIT-PayrollSubmit'),
  buildResourceKey('route', 'PIT-PayrollFeedback'),
  buildResourceKey('route', 'FTT-BasicData'),
  buildResourceKey('route', 'FTT-TaxType'),
  buildResourceKey('route', 'SBS-Server'),
  buildResourceKey('route', 'SBS-ServicePackageTemplate'),
  buildResourceKey('route', 'SBS-Customer'),
  buildResourceKey('route', 'SBS-CallLOG'),
  buildResourceKey('route', 'SBS-CallStatistics'),
  buildResourceKey('route', 'SBS-LogDetail'),
  buildResourceKey('route', 'SBS-Analyze')
]

const users: User[] = [
  {
    id: 'admin',
    name: 'Super Admin',
    introduction: '我是管理员',
    avatar: './img/smile-fill.png',
    subsystems: subsystemKeys,
    roles: [buildRoleKey('admin'), buildRoleKey('editor')],
    resources: resourceKeys
  },
  {
    id: 'editor',
    name: 'Normal Editor',
    introduction: '我是编辑',
    avatar: './img/smile-fill.png',
    subsystems: subsystemKeys,
    roles: [buildRoleKey('editor')],
    resources: resourceKeys
  }
]

const userIndexMapping = new Map<string, number>([
  ['admin', 0],
  ['editor', 1]
])

export const login = (req: Request, res: Response) => {
  const { username } = req.body
  const index = userIndexMapping.get(username)
  if (index === undefined) {
    return res.json({
      code: 500,
      messages: [`用户 [${username}] 不存在`]
    })
  } else {
    return res.json({
      code: 200,
      responseBody: username
    })
  }
}

export const logout = (req: Request, res: Response) => {
  return res.json({
    code: 200
  })
}

export const sync = (req: Request, res: Response) => {
  const tokenKey = getTokenKey()
  const username = req.headers[tokenKey] // Request 中的键值对建默认会转换为小写，为了避免麻烦，tokenKey 约定使用小写字母
  const index = userIndexMapping.get(username as string)
  if (index === undefined) {
    return res.json({
      code: 500,
      messages: [`用户 [${username}] 不存在`]
    })
  } else {
    return res.json({
      code: 200,
      responseBody: users[index]
    })
  }
}

export const changePassword = (req: Request, res: Response) => {
  return res.json({
    code: 500,
    messages: ['模拟修改密码.开始', '密码修改失败', '模拟修改密码.结束']
  })
}
